<?php
@session_start();
$selfpagearr=explode("/",$_SERVER["PHP_SELF"]);
$pagename=$selfpagearr[count($selfpagearr)-1];
if(!strstr($pagename,".php"))
{
    $pagename="index.php";
}
extract($_POST);

require_once "../lib/config.php";
require_once "../lib/functions.php";
require_once "../lib/pdomanager.php";
require_once "../lib/paginate_class.php";

require_once("../lib/newsfeed_functions.php"); 
include_once "../updates/ReadNewsFeed.php";
        
$newsFeedDetails=new ReadFeedAction();
$recordsToread=10;
if(
    isset($_POST["uid"]) && is_numeric(trim($_POST["uid"])) && trim($_POST["uid"])>0
    && isset($_POST["feedsetting"]) && is_numeric(trim($_POST["feedsetting"])) && trim($_POST["feedsetting"])>0
    && isset($_POST["feedid"]) && is_numeric(trim($_POST["feedid"])) //&& trim($_POST["feedid"])>=0
){
    //echo "inside...";
    $feedFor=trim($_POST["uid"]);
    $feedSetting=trim($_POST["feedsetting"]);
    $lastFeedID=trim($_POST["feedid"]);
    $recordsToread=trim($_POST["nFeedsToRead"]);
    $whichPostsToRead=trim($_POST["postTypeToRead"]);
    $parentid=trim($_POST["parentid"]);
    $strSearch=trim($_POST["sSearch"]);
    
    $clusteredId=0;
    //echo "...".$strSearch;
    $isJustACountRequest=false;
    if(isset($_POST["iscountreq"])){
        $isJustACountRequest=trim($_POST["iscountreq"]);
        if(is_numeric($isJustACountRequest)){
            $isJustACountRequest=true;
        }
    }
    if(!is_numeric($whichPostsToRead)){
        $whichPostsToRead=1;
    }
    
    $oLog= new Log();    
    //$oLog->writeVerbose('ajax/read_newsfeeds.php =>  last feed id read from => '.$lastFeedID.' \t');
    /*$latestFeeds=$newsFeedDetails->ReadStatusUpdates(
                                $loggedUserObj->userId,
                                $loggedUserObj->privacyType["Default_Setting"],
                                $loggedUserObj->lastupdatedfeedtimeread
                                );
    */                    
    if(!isset($parentid) || !is_numeric($parentid)){
        $parentid=0;
    }
    //echo "feedfor: ".$feedFor." \n feedsetting: ".$feedSetting."\n lastfeedid: ".$lastFeedID."\n recordtoread: ".($recordsToread+1);
    //echo "\n whichposttoread: ".$whichPostsToRead."\n parentid: ".$parentid;        
    $latestFeeds=$newsFeedDetails->ReadStatusUpdates(
                                        $feedFor,
                                        $feedSetting,
                                        $lastFeedID,
                                        ($recordsToread+1),
                                        $whichPostsToRead,
                                        $parentid,
                                        $clusteredId,
                                        $strSearch
                                   );                                
    //echo "<pre>";
//    print_r($latestFeeds);
//    echo "</pre>";
    $feedCount=0;
    $lastreadpostid=0;
    $totalFeedsRead=count($latestFeeds);
    
    if($isJustACountRequest){
        echo "_".$totalFeedsRead."_";
        die();
    }
    
    if($totalFeedsRead>0){
        //for($i=$totalFeedsRead-1;$i>=0;$i--){
//            $newsFeeds=$latestFeeds[$i];
//            $feedCount++;
//            
//            if($feedCount<$recordsToread){
//                $lastreadpostid=$newsFeeds["Post_ID"];
//                if($feedCount%2==0){
//                    //even
//                    echo getNewsFeedRight($feedCount,$newsFeeds);
//                }
//                else{
//                    //odd
//                    echo getNewsFeedLeft($feedCount,$newsFeeds);
//                }
//            }
//        }
        foreach($latestFeeds as $newsFeeds){
            $feedCount++;
            if($feedCount<$recordsToread){
                if(isset($newsFeeds["WatcherID"])){
                    if($lastreadpostid<$newsFeeds["WatcherID"]){
                        $lastreadpostid=$newsFeeds["WatcherID"];
                    }                    
                }
                else{
                    $lastreadpostid=$newsFeeds["Post_ID"];    
                }
                if($lastreadpostid<$lastFeedID){
                    $lastreadpostid=$lastFeedID;
                }
                if($feedCount%2==0){
                    //even
                    echo getNewsFeedRight($feedCount,$newsFeeds);
                }
                else{
                    //odd
                    echo getNewsFeedLeft($feedCount,$newsFeeds);
                }
            }        
        }   
        
        if($lastFeedID<=0){    
            $lastUpdatedRow= "<div id='lstfileidrow' style='display:none'>";
                $lastUpdatedRow.= "<div id='lstfileidtd'>".$lastreadpostid."</div>";
            $lastUpdatedRow.= "</div>";
        
            $lastUpdatedRow.= "<div id='rFinds' style='display:none'>"; // is view more needed or all data has been read
            if($totalFeedsRead>$recordsToread){
                $lastUpdatedRow.="1";
            }
            else{
                $lastUpdatedRow.="0";
            }
            $lastUpdatedRow.= "</div>";
            echo $lastUpdatedRow;
        }
         
    }
    else{
        echo "400";
    }   
}


?>